package cn.sc.jedis;

import java.util.*;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

/**
 * @ClassName JedisClusterFactory
 * @Author 谢卓才
 * @Description redis集群的操作
 * @Date 2022/11/13 9:17
 */
public class JedisClusterPool extends BinaryJedisCluster {


    public JedisClusterPool(HostAndPort node) {
        this(Collections.singleton(node));
    }

    public JedisClusterPool(HostAndPort node, int timeout) {
        this(Collections.singleton(node), timeout);
    }

    public JedisClusterPool(HostAndPort node, int timeout, int maxAttempts) {
        this(Collections.singleton(node), timeout, maxAttempts);
    }

    public JedisClusterPool(HostAndPort node, GenericObjectPoolConfig poolConfig) {
        this(Collections.singleton(node), poolConfig);
    }

    public JedisClusterPool(HostAndPort node, int timeout, GenericObjectPoolConfig poolConfig) {
        this(Collections.singleton(node), timeout, poolConfig);
    }

    public JedisClusterPool(HostAndPort node, int timeout, int maxAttempts, GenericObjectPoolConfig poolConfig) {
        this(Collections.singleton(node), timeout, maxAttempts, poolConfig);
    }

    public JedisClusterPool(HostAndPort node, int connectionTimeout, int soTimeout, int maxAttempts, GenericObjectPoolConfig poolConfig) {
        this(Collections.singleton(node), connectionTimeout, soTimeout, maxAttempts, poolConfig);
    }

    public JedisClusterPool(HostAndPort node, int connectionTimeout, int soTimeout, int maxAttempts, String password, GenericObjectPoolConfig poolConfig) {
        this(Collections.singleton(node), connectionTimeout, soTimeout, maxAttempts, password, poolConfig);
    }

    public JedisClusterPool(HostAndPort node, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig) {
        this(Collections.singleton(node), connectionTimeout, soTimeout, maxAttempts, password, clientName, poolConfig);
    }

    public JedisClusterPool(Set<HostAndPort> nodes) {
        this((Set)nodes, 2000);
    }

    public JedisClusterPool(Set<HostAndPort> nodes, int timeout) {
        this((Set)nodes, timeout, 5);
    }

    public JedisClusterPool(Set<HostAndPort> nodes, int timeout, int maxAttempts) {
        this(nodes, timeout, maxAttempts, new GenericObjectPoolConfig());
    }

    public JedisClusterPool(Set<HostAndPort> nodes, GenericObjectPoolConfig poolConfig) {
        this((Set)nodes, 2000, 5, poolConfig);
    }

    public JedisClusterPool(Set<HostAndPort> nodes, int timeout, GenericObjectPoolConfig poolConfig) {
        this((Set)nodes, timeout, 5, poolConfig);
    }

    public JedisClusterPool(Set<HostAndPort> jedisClusterNode, int timeout, int maxAttempts, GenericObjectPoolConfig poolConfig) {
        super(jedisClusterNode, timeout, maxAttempts, poolConfig);
    }

    public JedisClusterPool(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, GenericObjectPoolConfig poolConfig) {
        super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, poolConfig);
    }

    public JedisClusterPool(Set<HostAndPort> nodes, String password) {
        this(nodes, 15000, 2000,5, new GenericObjectPoolConfig());
    }

    public JedisClusterPool(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, GenericObjectPoolConfig poolConfig) {
        super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, poolConfig);
    }

    public JedisClusterPool(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig) {
        super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, clientName, poolConfig);
    }


    public void close() {
        if (this.connectionHandler != null) {
            Iterator i$ = this.connectionHandler.getNodes().values().iterator();

            while(i$.hasNext()) {
                redis.clients.jedis.JedisPool pool = (redis.clients.jedis.JedisPool)i$.next();

                try {
                    if (pool != null) {
                        pool.destroy();
                    }
                } catch (Exception var4) {
                }
            }
        }

    }

    public byte[] hSet(final byte[] key, final byte[] field, final byte[] value) {
        byte[] result = (new JedisClusterCommand<byte[]>(this.connectionHandler, this.maxAttempts) {
            public byte[] execute(Jedis connection) {
                byte[] value = connection.get(key);
                connection.hset(key, field, value);
                return value;
            }
        }).runBinary(key);
        return result;
    }

}